import * as math from 'mathjs'
import * as THREE from 'three'

// 根据公式获取点位
export const getPoints = (
  expressionStr: string,
  options?: any,
): VerticesType[] => {
  if (expressionStr === '') return []
  const range = (options?.range || [-10, 10, 0.1, false]) as [
    number,
    number,
    number,
    boolean,
  ]
  const [start, end, step, includeEnd = false] = range
  const expression = math.parse(expressionStr)
  const xValue = math.range(start, end, step, includeEnd).toArray()

  const yValue = xValue.map((x) => expression.evaluate({ x }))
  return xValue.map((x, i) => [x, yValue[i], 0] as VerticesType)
}

export const transformPoints = (points: VerticesType[]) => {
  const geometry = new THREE.BufferGeometry()
  const material = new THREE.LineBasicMaterial({
    color: 0xffffff,
    linewidth: 1,
    transparent: true,
    opacity: 0.5,
  })
  geometry.setFromPoints(points.map((point) => new THREE.Vector3(...point)))

  return new THREE.Line(geometry, material)
}
